﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Imaginecup.gameState.malaria.component.util;
using imaginecup.information;
using imaginecup.objectManager;
using Imaginecup.gameState.malaria.componentInit;
using Imaginecup.util;
using imaginecup.util;
using Imaginecup.sprite.changeSourceSize;
using Imaginecup.util.script.malaria;
using Imaginecup.util.resourceMan;
using Imaginecup.gameState.malaria.component.entity.effect;
#if WINDOWS_PHONE
using Microsoft.Devices;
#endif

namespace Imaginecup.gameState.malaria.component.entity.item
{
    /// <summary>
    /// 이것은 IUpdateable을 구현하는 게임 구성 요소입니다.
    /// </summary>
    public class CmpMoveItem : BaseCoolTimeItemEntityComponent//, ISourceInfoClass
    {
        private List<ICmpMalaria> m_MalariaList;
        private ICmpMalaria m_SelectedMalaria;

        private AttackInfo m_AttackInfo;

        private HandEffect m_HandEffect = null;

        private bool m_bSelect;
        private double m_UseAbleTime;
        private double m_UsingCalculateTime;


        public CmpMoveItem(GObjectID a_GID, ObjectManager objMan)
            : base(a_GID, objMan, ComponentTypeID.CID_ITEM_ENTITY)
        {
            m_MalariaList = new List<ICmpMalaria>();
            m_AttackInfo = new AttackInfo();
            m_AttackInfo.attackID = AttackID.ATTACK_MOVE;
            m_AttackInfo.extraData = m_MalariaList;

            m_SelectedMalaria = null;
            m_bSelect = false;
            

            m_CoolTime = 3;
            m_UseAbleTime = 20;

//#if WINDOWS
//            m_CoolTime = ScriptReader.GetScriptReader().GetDataToFloat("잡아끌기쿨타임");
//            m_UseAbleTime = ScriptReader.GetScriptReader().GetDataToFloat("잡아끌기시간");
//#endif
        }


        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

        

#if WINDOWS_PHONE
        public override EMessageResult HandleMessage(GMessage a_Msg)
        {
            if (a_Msg.m_Type.MessageTypeID == MessageTypeID.MT_CHECK_INPUT.MessageTypeID)
            {
                if (!m_bSelect) return base.HandleMessage(a_Msg);

                IMouseInputHandler input = MouseInputHandler.GetInputHandler();

                
                EntityInfo entityInfo = (EntityInfo)m_SelectedMalaria.GetEntityInfo();

                if( input.MouseState.LeftButton == ButtonState.Pressed )
                {
                    entityInfo.mPosition = new Vector2(input.Position.X - entityInfo.mSize.X / 2 ,
                                                       input.Position.Y - entityInfo.mSize.Y / 2);

                    VibrateMan.GetVibrateMan().Start();

                    GameTime gameTime = (GameTime)a_Msg.m_Data;
                    m_UsingCalculateTime -= gameTime.ElapsedGameTime.TotalSeconds;

                    if (m_UsingCalculateTime < 0)
                    {
                        m_UsingCalculateTime = 0;
                        SetClear();
                        m_CoolCalculateTime = m_CoolTime;
                        m_State = CoolState.Delay;
                        m_bEnd = true;
                        m_bSelectItem = false;
                    }
                }
                else if(input.MouseState.LeftButton == ButtonState.Released )
                {
                    entityInfo.mPosition = new Vector2(input.Position.X - entityInfo.mSize.X / 2,
                                                       input.Position.Y - entityInfo.mSize.Y / 2);
                    m_bSelect = false;
                    m_SelectedMalaria.ReleaseStop();

                    if( m_HandEffect != null )
                        {
                            m_ObjectMan.DestroyObject(m_HandEffect.GetObjectID());
                        }
                    VibrateMan.GetVibrateMan().Stop(); ;
                    RenderOff();
                }

                m_SelectedMalaria.SetEntityInfo(entityInfo);
            }
            return base.HandleMessage(a_Msg);
        }
#endif
#if WINDOWS


        public override EMessageResult HandleMessage(GMessage a_Msg)
        {
            if (a_Msg.m_Type.MessageTypeID == MessageTypeID.MT_CHECK_INPUT.MessageTypeID)
            {
                if (!m_bSelect) return base.HandleMessage(a_Msg);

                IMouseInputHandler input = MouseInputHandler.GetInputHandler();

                
                EntityInfo entityInfo = (EntityInfo)m_SelectedMalaria.GetEntityInfo();

                if( input.MouseState.LeftButton == ButtonState.Pressed )
                {
                    entityInfo.mPosition = new Vector2(input.Position.X - entityInfo.mSize.X / 2 ,
                                                       input.Position.Y - entityInfo.mSize.Y / 2);

                    GameTime gameTime = (GameTime)a_Msg.m_Data;
                    m_UsingCalculateTime -= gameTime.ElapsedGameTime.TotalSeconds;

                    if (m_UsingCalculateTime < 0)
                    {
                        m_UsingCalculateTime = 0;
                        SetClear();
                        m_CoolCalculateTime = m_CoolTime;
                        m_State = CoolState.Delay;
                        m_bEnd = true;
                        m_bSelectItem = false;
                    }

                }
                else if(input.MouseState.LeftButton == ButtonState.Released )
                {
                    entityInfo.mPosition = new Vector2(input.Position.X - entityInfo.mSize.X / 2,
                                                       input.Position.Y - entityInfo.mSize.Y / 2);
                    m_SelectedMalaria.ReleaseStop();
                    ReleaseItem();
                }

                m_SelectedMalaria.SetEntityInfo(entityInfo);
            }

            else if (a_Msg.m_Type.MessageTypeID == MessageTypeID.MT_MALARIA_DIE.MessageTypeID )
            {
                if( m_SelectedMalaria != null && m_SelectedMalaria.GetObjectID() == a_Msg.m_FromID )
                {
                    m_SelectedMalaria = null;

                    ReleaseItem();
                }
            }

            return base.HandleMessage(a_Msg);
        }
#endif
        private void ReleaseItem()
        {
            m_bSelect = false;

            if (m_HandEffect != null)
            {
                m_ObjectMan.DestroyObject(m_HandEffect.GetObjectID());
            }

            RenderOff();
        }


        public override bool IsUseAble()
        {
            if (m_State == CoolState.None)
            {
                m_UsingCalculateTime = m_UseAbleTime;
            }

            if( base.IsUseAble() == false ) 
                return false;
            
          
            IMouseInputHandler input = MouseInputHandler.GetInputHandler();          

            if (input.IsOneClick())
            {
                GMessage message = new GMessage(MessageTypeID.MT_CHECK_ATTACK);
                //마우스 충돌이 일어난 엔티티들을 등록할 수 있다.
                message.m_Data = m_AttackInfo;

                m_ObjectMan.BroadCastMessage(message, ComponentTypeID.CID_MALARIA_ENTITY);

                if (m_MalariaList.Count > 0)
                {
                    m_bSelect = true;
                    return true;
                }

            }
            return false;
        }

        protected override void SetClear()
        {
            if (m_bSelect)
            {
                m_bSelect = false;
                m_SelectedMalaria.ReleaseStop();

                if (m_HandEffect != null)
                {
                    m_ObjectMan.DestroyObject(m_HandEffect.GetObjectID());
                }
            }
        }

        public override void Release()
        {
            base.Release();
        }

        public override void UseItem(GameTime gameTime)
        {
            if( m_HandEffect != null )
            {
                m_ObjectMan.DestroyObject(m_HandEffect.GetObjectID());
            }

            m_HandEffect = new HandEffect(new GObjectID(), m_ObjectMan);
            m_ObjectMan.AddComponent(m_HandEffect);

            m_SelectedMalaria = m_MalariaList[0];
            m_MalariaList.Clear();
            m_SelectedMalaria.StopMove();
                      
        }


    }
}







//        public override EMessageResult HandleMessage(GMessage a_Msg)
//        {
//            if (a_Msg.m_Type.MessageTypeID == MessageTypeID.MT_CHECK_INPUT.MessageTypeID)
//            {
//                if (!m_bSelect) return base.HandleMessage(a_Msg);

//                IMouseInputHandler input = MouseInputHandler.GetInputHandler();

                
//                EntityInfo entityInfo = (EntityInfo)m_SelectedMalaria.GetEntityInfo();
//#if !XBOX
//                if( input.MouseState.LeftButton == ButtonState.Pressed )
//                {
//                    entityInfo.mPosition = new Vector2(input.Position.X - entityInfo.mSize.X / 2 ,
//                                                       input.Position.Y - entityInfo.mSize.Y / 2);

//#if WINDOWS_PHONE
//                    VibrateController.Default.Start(TimeSpan.FromSeconds(1));
//#endif
//                }
//                else if(input.MouseState.LeftButton == ButtonState.Released )
//                {
//                    entityInfo.mPosition = new Vector2(input.Position.X - entityInfo.mSize.X / 2,
//                                                       input.Position.Y - entityInfo.mSize.Y / 2);
//                    m_bSelect = false;
//                    m_SelectedMalaria.ReleaseStop();

//#if WINDOWS_PHONE
//                    VibrateController.Default.Stop();
//#endif
//                }

//                m_SelectedMalaria.SetEntityInfo(entityInfo);
//            }
//#else
//                if (input.GamePadState.Buttons.A == ButtonState.Pressed)
//                {
//                    entityInfo.mPosition = new Vector2(input.Position.X - entityInfo.mSize.X / 2,
//                                                       input.Position.Y - entityInfo.mSize.Y / 2);
//                }
//                else if (input.GamePadState.Buttons.A == ButtonState.Released)
//                {
//                    entityInfo.mPosition = new Vector2(input.Position.X - entityInfo.mSize.X / 2,
//                                                       input.Position.Y - entityInfo.mSize.Y / 2);
//                    m_bSelect = false;
//                    m_SelectedMalaria.ReleaseStop();

//                }
//                m_SelectedMalaria.SetEntityInfo(entityInfo);
//            }
//#endif

//            return base.HandleMessage(a_Msg);
//        }